package org.maxkey.portal.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.maxkey.portal.core.cam.PageResultData;
import org.maxkey.portal.core.cam.ResultData;
import org.maxkey.portal.core.search.ParamModel;
import org.maxkey.portal.core.search.model.UserParam;
import org.maxkey.portal.model.UserInfo;
import org.maxkey.portal.service.UserInfoService;
import org.maxkey.portal.repository.UserInfoRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class UserInfoServiceImpl implements UserInfoService {

    @Autowired
    UserInfoRepository userInfoRepository;

    @Override
    public ResultData findById(Integer id) {
        try {
            if(id==null){
                return ResultData.error("参数错误");
            }
            Optional<UserInfo> u= userInfoRepository.findById(id.intValue());
            if(!u.isEmpty()){
                return ResultData.ok(u);
            }
            return ResultData.error("查询不存在");
        }catch (Exception e){
            e.printStackTrace();
            return ResultData.error(e.getMessage());
        }
    }

    @Override
    public ResultData findUserByLoginname(String Loginname) {
       if(StringUtils.isNotEmpty(Loginname)){
           UserInfo user=userInfoRepository.findUserByLoginname(Loginname);
           if(user!=null){
               return ResultData.ok(user);
           }
       }
       return ResultData.error("查询不存在");
    }

    /*

        @Override
        public UserInfo findUserByLoginname(String Loginname) {
            return userInfoRepository.findUserByLoginname(Loginname);
        }
    */
    @Override
    public ResultData findAll() {
        try {
            return ResultData.ok(userInfoRepository.findAll());
        }catch (Exception e){
            e.printStackTrace();
            return ResultData.error(e.getMessage());
        }
    }

    @Override
    public PageResultData findList(ParamModel<UserParam> searData) {
        try {
            PageRequest pageRequest = PageRequest.of(searData.getPageNo(), searData.getPageSize());
            Specification<UserInfo> specification = (Specification<UserInfo>) (root, query, criteriaBuilder) -> {
                List<Predicate> list = new ArrayList<>();
                // 第一个userId为CloudServerDao中的字段，第二个userId为参数
                if(!StringUtils.isEmpty(searData.getCondition().getName())){
                    Predicate predicate1 = criteriaBuilder.like(root.get("name").as(String.class), "%" + searData.getCondition().getName() + "%");
                    list.add(predicate1);
                }
                if(!StringUtils.isEmpty(searData.getCondition().getLoginName())){
                    Predicate predicate2 = criteriaBuilder.like(root.get("loginname").as(String.class), "%" + searData.getCondition().getLoginName() + "%");
                    list.add(predicate2);
                }
                if(searData.getCondition().getStatus() != 0)
                {
                    Predicate predicate3 = criteriaBuilder.equal(root.get("status").as(Integer.class), searData.getCondition().getStatus());
                    list.add(predicate3);
                }
                return criteriaBuilder.and(list.toArray(new Predicate[0]));
            };
            Page<UserInfo> p=userInfoRepository.findAll(specification,pageRequest);
            return PageResultData.ok(Integer.valueOf(p.getTotalElements()+"") ,p.getContent());
        }catch (Exception e){
            e.printStackTrace();
            return PageResultData.error(e.getMessage());
        }
    }

    @Override
    public ResultData deleteById(Long id) {
        try {
            if(id==null){
                return ResultData.error("参数错误");
            }
            userInfoRepository.deleteById(id.intValue());
            return ResultData.ok();
        }catch (Exception e){
            e.printStackTrace();
            return ResultData.error(e.getMessage());
        }
    }

    @Override
    public ResultData save(UserInfo userInfo) {
        try {
            if(userInfo.getCreateTime()==null){
                userInfo.setCreateTime(new Date());
            }
            if(userInfo.getUpdateTime()==null){
                userInfo.setUpdateTime(new Date());
            }
            userInfoRepository.save(userInfo);
            return ResultData.ok();
        }catch (Exception e){
            e.printStackTrace();
            return ResultData.error(e.getMessage());
        }
    }

}
